package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.model.common.AuthParam;
import com.cdks.wuxiu.api.model.param.ScoreGetRecordParam;
import com.cdks.wuxiu.api.model.vo.ScoreHomeVO;
import com.cdks.wuxiu.api.service.ScoreService;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.UserBillInfo;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "积分")
@Slf4j
@RestController
@RequestMapping("/score")
public class ScoreController {
    @Resource
    private ScoreService scoreService;
            

    @ApiOperation("积分主页")
    @PostMapping("/home")
    public ApiResult<ScoreHomeVO> home(@RequestBody @Valid AuthParam param) {
        ScoreHomeVO vo  = new ScoreHomeVO();
        vo.setBalance(scoreService.getBalance(param.getUserId()));
        vo.setBillInfo(scoreService.getRecord(param.getUserId()));
        vo.setRule("https://wuxiu.cdks.net/web/score/rule.html");

        return ApiResult.success(vo);
    }

    @ApiOperation("获取积分记录")
    @PostMapping("/getRecord")
    public ApiResult<Object> getRecord(@RequestBody @Valid ScoreGetRecordParam param) {
        List<UserBillInfo> recordList  = scoreService.getRecord(param.getUserId(), param.getPage(), param.getStart(), param.getStop());
        
        JSONObject json  = new JSONObject();
        json.set("recordList", recordList);

        return ApiResult.success(json);
    }
}
